package DataStructures;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * This class contains the functionality necessary for a poker table, that is:
 * 	Dealer chip location
 * 	Chips in front of players
 * 	Cards on the table
 * @author Phil
 *
 */
public class PokerTable extends Table{	
	private ArrayList<Card> cards;
	
	private int chipsInMiddle;
	private int[] chipsInFrontOfPlayer;
	private int tableId;
	private int dealerButtonLocation;
	
	public PokerTable(int numberOfSeats){
		super(numberOfSeats);
		chipsInFrontOfPlayer = new int[numberOfSeats];
		chipsInMiddle = 0;
		dealerButtonLocation = -1;
		
		cards = new ArrayList<Card>();
		tableId = new Random().nextInt();
	}
	
	//**********
	// General methods
	//*********
	
	/**
	 * This function removes the cards  and chips from the table
	 */
	public void clearTable(){
		this.clearCardsOnTable();
		this.clearChipsOnTable();
	}
	
	public int getTableId(){
		return tableId;
	}
	
	
	//***************************
	//  Section for interacting with cards on the table
	//***************************
	
	/**
	 * Put a card on the table
	 */
	public void placeCardOnTable(Card card){
		this.cards.add(card);
	}
	
	/**
	 * Get the cards on the table
	 * @return - An array list containing the cards in play
	 */
	public ArrayList<Card> getCardsOnTable(){
		return this.cards;
	}
	
	/**
	 * Remove cards from the table
	 */
	public void clearCardsOnTable(){
		this.cards.clear();
	}
		
	//***************************
	//  Section for interacting with chips on the table
	//***************************
	
	/**
	 * Put an amount of chips in front of a player. This is used for chips that are taken from
	 * the players stack but are not yet in the pot. For example, a raise.
	 */
	public void placeChipsInFrontOfPlayer(int seat, int amount){
		if(seat < 0 || seat >= this.getTableSize()){
			throw new IllegalArgumentException("Cannot place chips in front of player at seat " + seat + " , there is " + this.getTableSize() + " seats");
		}
		this.chipsInFrontOfPlayer[seat] += amount;
		//Note: The dealer is responsible for getting these chips from the player
	}
	
	/**
	 * Put all the chips in front of players and place them in the pot
	 */
	public void collectChipsIntoMiddle(){
		for(int i=0; i<this.chipsInFrontOfPlayer.length; i++){
			this.chipsInMiddle += this.chipsInFrontOfPlayer[i];
			this.chipsInFrontOfPlayer[i] = 0;
		}
	}	
	
	/**
	 * Get the chip count in the pot. 
	 * @return
	 */
	public int getChipsInMiddle(){
		return this.chipsInMiddle;
	}
	
	/**
	 * Get the total number of chips in the pot and in front of players
	 * @return
	 */
	public int getTotalChips(){
		int totalChips = chipsInMiddle;
		for(int i=0; i<this.chipsInFrontOfPlayer.length; i++){
			totalChips += this.chipsInFrontOfPlayer[i];			
		}
		return totalChips;
	}
	
	/**
	 * This removes all chips from the middle of the table and all chips in front of the players
	 */
	public void clearChipsOnTable(){
		this.chipsInMiddle = 0;
		for(int i=0; i<this.chipsInFrontOfPlayer.length; i++){		
			this.chipsInFrontOfPlayer[i] = 0;
		}		
	}
	
	//***************************
	//  Section for interacting with the dealer button 
	//***************************
	
	/**
	 * Give the dealer button to a player at the specified seat
	 * If the seat number is invalid this method will return false
	 */
	public boolean placeDealerButton(int seat){
		boolean successful = false;
		if(seat < 0 || seat >= this.getTableSize()){
			// Check the seat number is valid
			successful = false; 
		}else if(isSeatEmpty(seat)){
			// Ensure a player is sitting at the seat
			successful = false;
		}else {
			// Give the player the dealer button
			this.dealerButtonLocation = seat;
			successful = true;
		}
		return successful;
	}
	
	
	
	/**
	 * Returns the location of the dealer button, if the dealer button has 
	 * not been placed yet then -1 is returned
	 * @return
	 */
	public int getDealerButtonLocation(){
		return this.dealerButtonLocation;
	}
	
	/**
	 * Pass the dealer button to the player in the next seat clockwise. If the dealer
	 * button has not been set then it passes the button to the player in the lowest seat number
	 */
	public void passDealerButtonClockwise(){
		this.dealerButtonLocation += 1;		
		this.dealerButtonLocation = this.dealerButtonLocation % this.getTableSize();
		if(isSeatEmpty(dealerButtonLocation)){
			passDealerButtonClockwise(); // if seat is empty try passing the button clockwise again
		}		
	}	
	
	
	//***************************
	//  Section for interacting with the poker players button 
	//***************************
	public PokerPlayer getPlayerUnderTheGun(){
		return null;
	}
	
	/**
	 * Return the player in the big blind position
	 */
	public PokerPlayer getPlayerInBigBlind(){
		
		return null;
	}
	
	
	/**
	 * Return the player in the small blind position
	 * @return
	 */
	public PokerPlayer getPlayerInSmallBlind(){
		
		return null;
	}
}
